<?php
namespace App\Http\Controllers\Store;

use App\Http\Requests\BasicRequest;
use App\Models\ShopOrder;
use App\Models\ClientUser;
use App\Models\ClientUserAddress;
use App\Models\House;
use Illuminate\Support\Facades\DB;
class ClientController extends CommonController {

    public function client_list(BasicRequest $request){

        $search = $request->input('search');
        $client_list = ShopOrder::select(DB::raw('order_sn,uid,user_name,user_mobile,count(*) as buy_num,sum(`order_amount`) as buy_amount'))
            ->where(['store_id'=>$this->store_id])
            ->groupBy('uid')
            ->orderBy('id','desc')
            ->when($search,function($query) use ($search){
                $query->where(function($query)use ($search){
                    $query->where('user_name','like','%'.$search.'%')->orWhere('user_mobile','like','%'.$search.'%');
                });
            })
            ->pages()->each(function($v,$k){
                $house_id=ClientUserAddress::where(['uid'=>$v->uid])->orderBy('is_default','desc')->value('house_id');
                $v->house_name = House::where(['house_id'=>$house_id])->value('house_detail');
                $v->avatar = ClientUser::where(['id'=>$v->uid])->value('avatar');
            });

			
        return json_success('OK',$client_list);
    }

}